package conan.dataSource;

import conan.DomainLayer.Kunde;


import java.sql.*;

import java.util.*;


public class UnitOfWork
{
    public KundeMapper kundeMapper;

    private ArrayList<Kunde> newKunde;
    private ArrayList<Kunde> dirtyKunde;

    public UnitOfWork()
    {
        kundeMapper = new KundeMapper();
        newKunde = new ArrayList<Kunde>();
        dirtyKunde = new ArrayList<Kunde>();
    }

    public void registerNewKunde(Kunde kunde)
    {
        if // not allready registered in any list
            (!newKunde.contains(kunde) && !dirtyKunde.contains(kunde))
        {
            newKunde.add(kunde);
        }
        
    }

    public void registerDirtyKunde(Kunde kunde)
    {
        if // not allready registered in any list
            (!newKunde.contains(kunde) && !dirtyKunde.contains(kunde))
        {
            dirtyKunde.add(kunde);
        }
    }


    public boolean commit() throws SQLException
    {
        Connection conn = null;
        boolean status = true;
        try
        {
            conn = DB.getConnection();
            //=== system transaction starts
            conn.setAutoCommit(false);

            status = status && kundeMapper.insertKunde(newKunde, conn);
            status = status && kundeMapper.updateKunde(dirtyKunde, conn);


            //=== system transaction ends with success
            conn.commit();
        }
        catch (Exception e)
        {
            System.out.println("fejl i UnitOfWork - commit()");
            System.err.println(e);
            //=== system transaction ends with roll back
            conn.rollback();
            status = false; // rettelse
        }
        finally
        {
            DB.releaseConnection(conn);
        }
        return status;
    }

    //====== read from DB ===================================================

    public Kunde getKunde(int kundeId)
    {
        Connection conn = null;
        Kunde kunde = null;

        try
        {
            conn = DB.getConnection();
            kunde = kundeMapper.getKunde(kundeId, conn);
        }
        catch (Exception e)
        {
            System.out.println("fejl i UnitOfWork - getKunde()");
            System.err.println(e);
        }
        finally
        {
            DB.releaseConnection(conn);
        }
        return kunde;

    }
    //=== get next unique ono from DB-sequence

    public int getNextKundeNo()
    {
        Connection conn = null;
        int nextOno = 0;
        try
        {
            conn = DB.getConnection();
            nextOno = kundeMapper.getNextKundeNo(conn);
        }
        catch (Exception e)
        {
            System.out.println("fejl i UnitOfWork - getNextKundeNo()");
            System.err.println(e);
        }
        finally
        {
            DB.releaseConnection(conn);
        }
        return nextOno;
    }
        
    public String toString()
    {
        String ret = "****UNIT OF WORK Start****";
        
        ret += "newKunde:"+ "\n";
        for (int i = 0; i < newKunde.size(); i++) 
        {
            ret += newKunde.get(i) + "\n";
        }
        
        ret += "dirtyKunde:"+ "\n";
        for (Kunde i : dirtyKunde) 
        {
            ret += i + "\n";
        }
        
        
        
        ret += "****UNIT OF WORK End****";
        return ret;
    }
}
